/* 操作界面主面板 */
Divo.app.MainPanel = function(config) {
  Ext.apply(this, config);

  var html = ['<div id="welcome-div">', '<div style="float:left;"></div>',
      '<div>', '<h2>公告：欢迎使用！</h2>', '<p></p>', '</div>'];

  Divo.app.MainPanel.superclass.constructor.call(this, {
        id : 'menu-content-panel',
        region : 'center',
        margins : '3 3 3 0',
        resizeTabs : true,
        minTabWidth : 90,
        tabWidth : 150,
        enableTabScroll : true,
        activeTab : 0,
        deferredRender : false,
        items : [{
              id : 'welcome-panel',
              title : '待办事项',
              layout : 'column',
              bodyStyle : 'padding:25px',
              html : html.join(''),
              border : 1,
              autoScroll : true,
              items : []
            }]
      });
}

Ext.extend(Divo.app.MainPanel, Ext.TabPanel, {
  cfg_run_mode : 'DEV',// 默认为dev,可通过config实现设置
  build_id : '',// 版本标识,方便js,css去除缓存
  NEW_LINE : '\n',
  loadDefault : function() {
    var me = this;
    function buttonClick(id) {
      var href, menuId, title, iconCls, module_name = "123";
      var sysConfig;
      Ext.Ajax.request({
            url : "common/getsystemconfig",
            scope : this,
            async : false,
            callback : function(o, s, r) {
              var resp = Ext.decode(r.responseText);
              sysConfig = {
                module_name : resp.resource.text
              }
            }
          });
      Ext.Ajax.request({
        params : {
          resourceId : id
        },
        url : "notice/query_resource",
        scope : this,
        async : true,
        callback : function(o, s, r) {
          var resp = Ext.decode(r.responseText);
          if (resp.success) {
            href = resp.resource.url;
            menuId = id;
            title = resp.resource.text;
            iconCls = resp.resource.iconCls;
            me.loadContent(href, menuId, title, iconCls, sysConfig.module_name);
          }
        }
      });

    }
    Ext.Ajax.request({
          url : "notice/query_notice_to",
          scope : this,
          async : true,
          callback : function(o, s, r) {
            var resp = Ext.decode(r.responseText);
            if (resp.totalCount == 0) {
            } else {
              var noticeRows = resp.rows;
              var panel = new Ext.Panel({
                    padding : 5,
                    id : 'tz-panel',
                    title : '待办事项',
                    width : '98%',
                    height : '100',
                    layout : 'column',
                    items : [{
                          layout : "form",
                          columnWidth : .5,
                          border : false,
                          id : "tz-panel-left",
                          items : []
                        }, {
                          layout : "form",
                          columnWidth : .5,
                          border : false,
                          id : "tz-panel-right",
                          items : []
                        }]
                  });
              Ext.getCmp('welcome-panel').items.add(panel);
              for (i = 0; i < resp.totalCount; i++) {
                var id = noticeRows[i].url;
                var button = new Ext.Button({
                      id : 'lb-' + i + "~" + id,
                      text : noticeRows[i].notice,
                      handler : function() {
                        var resourceIds = this.id;
                        var resourceId = resourceIds.substring(resourceIds
                            .indexOf("~")
                            + 1);
                        buttonClick(resourceId);
                      }
                    });
                if (i % 2 == 0) {
                  Ext.getCmp('tz-panel-left').items.add(button);
                  //Ext.getCmp('tz-panel-left').items.add("<did style='height:10px;'></div>");
                } else {
                  Ext.getCmp('tz-panel-right').items.add(button);
                }
              }
              Ext.getCmp('welcome-panel').doLayout();
              panel.doLayout();
            }
          }
        });
  },
  reloadWelcome : function() {
    var html = ['<div id="welcome-div">', '<div style="float:left;"></div>',
        '<div>', '<h2>公告：欢迎使用！</h2>', '<p></p>', '</div>'];

    Divo.app.MainPanel.superclass.constructor.call(this, {
          id : 'menu-content-panel',
          region : 'center',
          margins : '3 3 3 0',
          resizeTabs : true,
          minTabWidth : 90,
          tabWidth : 150,
          enableTabScroll : true,
          activeTab : 0,
          deferredRender : false,
          items : [{
                id : 'welcome-panel',
                title : '待办事项',
                layout : 'column',
                bodyStyle : 'padding:25px',
                html : html.join(''),
                border : 1,
                autoScroll : true,
                items : []
              }]
        });
        
  },
  loadContent : function(href, menuId, title, iconCls, module_name) {
    var ifrId = 'frame-' + menuId;
    var tabId = 'contantstab-' + menuId;
    var icls = iconCls;
    var f = document.getElementById(ifrId);
    var wf = window.frames[ifrId];

    if (Ext.isEmpty(f) && !Ext.isEmpty(wf)) {
      delete wf;
      delete window.frames[ifrId];// 仅仅删除wf对象,会导致关闭tabpanel后再打开时dom消失
    }

    if (!Ext.isEmpty(f)) {
      if (this.getActiveTab().getId() == tabId) {
        this.loadMenuLink(ifrId, href);
      } else {
        this.activate(tabId);
      }
    } else {
      // userlog

      Ext.Ajax.request({
            url : "system/user_log",
            params : {
              infos : '[' + module_name + ']' + title
            },
            scope : this
          });

      this.add(new Ext.Panel({
        title : title,
        id : tabId,
        autoScroll : true,
        iconCls : icls,
        layout : 'fit',
        closable : true,
        html : '<iframe id="'
            + ifrId
            + '" name="'
            + ifrId
            + '" src="about:blank" style="border:0;width:100%;height:100%;overflow: hidden" FRAMEBORDER="no"></iframe>'
      }));
      this.activate(tabId);
      this.loadMenuLink(ifrId, href);
    }

    this.doLayout();
  },
  loadMenuLink : function(ifrId, href) {
    var curIframe = window.frames[ifrId];

    var htmlString = this.buildHtml(href);
    curIframe.document.open("text/html", "replace");
    curIframe.document.write(htmlString);
    curIframe.document.close();
  },
  buildHtml : function(href) {
    var out = '';

    out += '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">'
        + this.NEW_LINE;
    out += '<html>' + this.NEW_LINE;
    out += this.buildHtmlHead(href);
    out += this.buildHtmlBody();
    out += '</html>' + this.NEW_LINE;
    out += '';
    return out;
  },
  buildHtmlHead : function(href) {
    var out = '';
    out += '<head>' + this.NEW_LINE;
    out += '  <META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=UTF-8"/>'
        + this.NEW_LINE;

    out += '  <meta http-equiv="pragma" content="no-cache">' + this.NEW_LINE;
    out += '  <meta http-equiv="cache-control" content="no-cache, must-revalidate">'
        + this.NEW_LINE;
    out += '  <meta http-equiv="expires" content="0">' + this.NEW_LINE;

    out += this.buildImportExtjs();
    out += this.buildImportEkingBase();
    out += this.buildImportDoc(href);
    out += '</head>' + this.NEW_LINE;
    out += '';
    return out;
  },
  buildHtmlBody : function() {
    var out = '';
    out += '<body>' + this.NEW_LINE;
    out += '</body>' + this.NEW_LINE;
    out += '';
    return out;
  },
  buildImportExtjs : function() {
    var out = '';
    var ts = '?ver=' + this.build_id;

    // if (CFG_DEPLOYMENT_TYPE == 'DEV') {
    // ts = '?_t_='+(new Date()).getTime();
    // }

    out += '  <link rel="stylesheet" type="text/css" href="resources/ext/resources/css/ext-all.css"/>'
        + this.NEW_LINE;
    out += '  <link rel="stylesheet" type="text/css" href="resources/css/default.css'
        + ts + '"/>' + this.NEW_LINE;
    out += ' <link rel="stylesheet" type="text/css" href="resources/css/app.css'
        + ts + '"/>' + this.NEW_LINE;
    out += ' <link rel="stylesheet" type="text/css" href="resources/css/portal.css'
        + ts + '"/>' + this.NEW_LINE; // 窗体拖拽
    out += ' <link rel="stylesheet" type="text/css" href="resources/css/ext-patch.css'
        + ts + '"/>' + this.NEW_LINE;
    out += '  <script type="text/javascript" src="resources/ext/ext-base.js'
        + ts + '" ><\/script>' + this.NEW_LINE;
    out += '  <script type="text/javascript" src="resources/ext/ext-all.js'
        + ts + '" ><\/script>' + this.NEW_LINE;
    out += '  <script type="text/javascript" src="resources/ext/ext-basex.js'
        + ts + '" ><\/script>' + this.NEW_LINE;
    out += '  <script type="text/javascript" src="resources/ext/ext-lang-zh_CN.js'
        + ts + '"><\/script>' + this.NEW_LINE;
    if (this.cfg_run_mode == 'DEV') {
      out += '  <script type="text/javascript" src="resources/ext/debug.js'
          + ts + '" ><\/script>' + this.NEW_LINE;
    }

    if (Ext.isIE) {
      out += '<script id="ie-deferred-loader" defer="defer" src="//:"></script>'
          + this.NEW_LINE;
    }

    out += '';
    return out;
  },
  buildImportEkingBase : function() {
    // var ts = '';
    var ts = '?ver=' + this.build_id;
    var out = '';

    out += '  <script type="text/javascript" src="resources/js/lib/common.lib.js'
        + ts + '"><\/script>' + this.NEW_LINE;
    out += '  <script type="text/javascript" src="resources/js/lib/utils.js'
        + ts + '"><\/script>' + this.NEW_LINE;
    out += '  <script type="text/javascript" src="resources/js/ux/BufferView.js'
        + ts + '"><\/script>' + this.NEW_LINE;
    out += '  <script type="text/javascript" src="resources/js/lib/app.base.grid.js'
        + ts + '"><\/script>' + this.NEW_LINE;
    out += '  <script type="text/javascript" src="resources/js/lib/app.base.form.js'
        + ts + '"><\/script>' + this.NEW_LINE;
    out += '  <script type="text/javascript" src="resources/js/lib/exportexecl.js'
        + ts + '"><\/script>' + this.NEW_LINE;
    out += '  <script type="text/javascript" src="resources/js/lib/app.base.component.js'
        + ts + '"><\/script>' + this.NEW_LINE;
    out += '  <script type="text/javascript" src="resources/js/lib/lib.js' + ts
        + '"><\/script>' + this.NEW_LINE;

    out += '';
    return out;
  },
  buildImportDoc : function(jsUI) {
    var basePath = 'resources/js/';
    var out = '';
    var ts = '?ver=' + this.build_id;
    // if (this.cfg_run_mode == 'DEV') {
    // ts ='';
    // }

    var pDcArray = uiDcInc.get(jsUI)
    if (pDcArray != undefined) {
      for (var j = 0; j < pDcArray.length; j++) {
        out += '<script  type="text/javascript" src="' + basePath + pDcArray[j]
            + ts + '"><\/script>' + this.NEW_LINE;
      }
    }

    out += '';
    return out;
  }
});